package com.gemme.gemme.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;

import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @Author: yangcheng
 * @CreateDate: 2020/9/22 11:36
 */
public class ShareUtil {
    public static void shareWinxin(Activity act, String imagestr, String url, String title, String desc){
        UMImage image = new UMImage(act, imagestr);//网络图片
        UMWeb web = new UMWeb(url);
        web.setTitle(title);//标题
        web.setThumb(image);  //缩略图
        web.setDescription(desc);//描述
//        userShareSaveData("WEChAT","https://new.qq.com/omn/20200709/20200709A05NXP00.html");
//        userShareSaveData("WEChAT",url);

        new ShareAction(act)
                .setPlatform(SHARE_MEDIA.WEIXIN)//传入平台
                .withText("hello")//分享内容
                .withMedia(web)
                .setCallback(new UMShareListener() {
                    @Override
                    public void onStart(SHARE_MEDIA share_media) {
                        System.out.println("分享 : onStart");
                    }

                    @Override
                    public void onResult(SHARE_MEDIA share_media) {
                        System.out.println("分享 : onResult");
                    }

                    public void onError(SHARE_MEDIA share_media, Throwable throwable) {
                        System.out.println("分享 : onError");
                    }

                    @Override
                    public void onCancel(SHARE_MEDIA share_media) {
                        System.out.println("分享 : onCancel");
                    }
                })//回调监听器
                .share();
    }

    /**
     * 保存图片到指定路径
     *
     * @param context
     * @param bitmap   要保存的图片
     * @param fileName 自定义图片名称
     * @return
     */
    public static File saveImageToGallery(Context context, Bitmap bitmap, String fileName) {
        // 保存图片至指定路径
        String storePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "qrcode";
        File appDir = new File(storePath);
        if (!appDir.exists()) {
            appDir.mkdir();
        }
        File file = new File(appDir, fileName);
        try {
            FileOutputStream fos = new FileOutputStream(file);
            //通过io流的方式来压缩保存图片(80代表压缩20%)
            boolean isSuccess = bitmap.compress(Bitmap.CompressFormat.JPEG, 80, fos);
            fos.flush();
            fos.close();

            //发送广播通知系统图库刷新数据
            Uri uri = Uri.fromFile(file);
            context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri));
            if (isSuccess) {
                return file;
            } else {
                return file;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }
}
